﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using edu.Service;
using edu.Model;

namespace edu.Web
{
    public partial class DailyBehaviorForTeacher : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlClassBind();
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemIndex+1) % 4 == 0)
            {
                e.Item.Controls.Add(new LiteralControl(" </tr> <tr><tr><td style=\"height: 10px\"></td></tr>"));
            }
        }

        protected void classDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            pager1.RecordCount = 0;
            pager1.CurrentPageIndex = 1;
            ViewState["loaded"] = false;
            loadData();
        }

        protected void submitImageBtn_Click(object sender, ImageClickEventArgs e)
        {
            cacheSelectedData();
            DataTable table = ViewState["table"] as DataTable;
            List<Model.DailyBehavior> list = new List<Model.DailyBehavior>();
            foreach (DataRow row in table.Rows)
            {
                Model.DailyBehavior obj = new Model.DailyBehavior();

                obj.Id = string.IsNullOrEmpty(row["dbId"].ToString()) ? 0 : Convert.ToInt32(row["dbId"]);
                obj.Year = DateTime.Now.Year;
                obj.Month = DateTime.Now.Month;
                obj.Day = DateTime.Now.Day;
                obj.StudentId = Convert.ToInt32(row["studentId"]);
                obj.BehaviorId = Convert.ToInt32(row["behaviorId"]);
                list.Add(obj);
            }
            DailyBehaviorService.Save(list);
            
            classDropDownList_SelectedIndexChanged(null, null);
        }

        protected void cancelImageBtn_Click(object sender, ImageClickEventArgs e)
        {

        }


        private void ddlClassBind()
        {
            DataTable tbClass = ClassService.GetClassByTeacherId(UserInfo.Id);
            
            foreach (DataRow row in tbClass.Rows)
            {
                int grade = (int)row["grade"];
                int classNum = (int)row["classNum"];
                string className = string.Format("{0}级{1}班", grade, classNum);
                ddlClass.Items.Add(new ListItem(className, row["Id"].ToString()));
            }

            if (tbClass.Rows.Count == 0)
            {
                ddlClass.Items.Insert(0, new ListItem("没有关联班级","-1"));
            }
            else
            {
                ddlClass.Items.Insert(0, new ListItem("请选择","-1"));
            }
            ddlClass.SelectedIndex = 1;
            classDropDownList_SelectedIndexChanged(null, null);
        }

        protected void pager1_PageChanged(object sender, EventArgs e)
        {
            cacheSelectedData();
            loadData();
        }

        /// <summary>
        /// 缓存当前页面的学生数据
        /// </summary>
        private void cacheSelectedData()
        {
            DataTable table = ViewState["table"] as DataTable;
            foreach (RepeaterItem item in Repeater1.Items)
            {
                RadioButton normalRadioBtn = item.FindControl("normalRadioBtn") as RadioButton;
                RadioButton abnormalRadioBtn = item.FindControl("abnormalRadioBtn") as RadioButton;
                HiddenField hidId = item.FindControl("hidId") as HiddenField;
                HiddenField hidSid = item.FindControl("hidSid") as HiddenField;

                DataRow row = table.Select(string.Format("studentId = {0}", hidSid.Value))[0];
                if (normalRadioBtn.Checked)
                {
                    row["behaviorId"] = 1;
                }
                if (abnormalRadioBtn.Checked)
                {
                    row["behaviorId"] = 2;
                }
            }
        }

        private void loadData()
        {
            int classId = Convert.ToInt32(ddlClass.SelectedValue);
            if (classId == -1)
            {
                Repeater1.DataSource = null;
                Repeater1.DataBind();
                return;
            }


            //建立缓存表
            
            if ((bool)ViewState["loaded"] == false)
            {
                DataTable data = DailyBehaviorService.GetCurrentlyList(classId);
                ViewState["table"] = data;
                pager1.RecordCount = data.Rows.Count;

                ViewState["loaded"] = true;
            }

            int pageSize = pager1.PageSize;
            int pageIdx = pager1.CurrentPageIndex;
            DataTable table = (ViewState["table"] as DataTable).Copy();
            for (int i = 0; i < (pageIdx - 1) * pageSize;i++ )
            {
                    table.Rows.RemoveAt(0);
            }

            int rowsCount = table.Rows.Count;
            for (int i = pageSize; i < rowsCount; i++)
            {
                table.Rows.RemoveAt(pageSize);
            }

            Repeater1.DataSource = table;
            Repeater1.DataBind();

            foreach (RepeaterItem item in Repeater1.Items)
            {
                RadioButton normalRadioBtn = item.FindControl("normalRadioBtn") as RadioButton;
                RadioButton abnormalRadioBtn = item.FindControl("abnormalRadioBtn") as RadioButton;

                if (table.Rows[item.ItemIndex]["behaviorId"].ToString() == "1")
                {
                    normalRadioBtn.Checked = true;
                }
                else if (table.Rows[item.ItemIndex]["behaviorId"].ToString() == "2")
                {
                    abnormalRadioBtn.Checked = true;
                }
                else  //没有选择默认正常
                {
                    normalRadioBtn.Checked = true;
                }
            }
        }
      
    }
}